package com.kaplandroid.smsforwarder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.EditText;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.Toast;




/**
 * 
 * @author KAPLANDROID
 * 
 */
public class SMSSettingActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	Button btnRehber, btnSend,btnSendPerulangan1, btnSendPerulangan2, btnSendPerulangan3, btnSendPerulangan4;
	Button btnClearPerulangan1, btnClearPerulangan2, btnClearPerulangan3, btnClearPerulangan4;
	EditText etPhoneNo,etNamaSaklar1,etNamaSaklar2, etNamaSaklar3, etNamaSaklar4, etDurasi1, etDurasi2, etDurasi3, etDurasi4;
	Switch switch1, switch2, switch3, switch4;
	CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
	
	//RadioButton rbOpen, rbClose;
	RelativeLayout rlForwardLayout;

	String DEBUG_TAG = "SMS Forwarder";

	private static final int PICK_CONTACT = 1001;
	public static final String KEY_NUMBER = "KEY_NUMBER";
	public static final String KEY_SHAREDNAME = "smsForward";
	public static final String KEY_SAKLAR1 = "Switch 1";
	public static final String KEY_SAKLAR2 = "Switch 2";
	public static final String KEY_SAKLAR3 = "Switch 3";
	public static final String KEY_SAKLAR4 = "Switch 4";
	private int iPerulangan;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
				
		//Setting Spinner Combo
		//Setting Spinner Combo Saklar 1
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.perulangan_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        //spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
        //Setting Spinner Combo Saklar 2
        Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                this, R.array.perulangan_array, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);
        //spinner4.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
        //Setting Spinner Combo Saklar 1
        Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
                this, R.array.perulangan_array, android.R.layout.simple_spinner_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner3.setAdapter(adapter);
        //spinner4.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
        //Setting Spinner Combo Saklar 4
        Spinner spinner4 = (Spinner) findViewById(R.id.spinner4);
        ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
                this, R.array.perulangan_array, android.R.layout.simple_spinner_item);
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner4.setAdapter(adapter);
        //spinner4.setOnItemSelectedListener(new MyOnItemSelectedListener());		
		
		etPhoneNo = (EditText) findViewById(R.id.etPhoneNo);
		etNamaSaklar1 = (EditText) findViewById(R.id.etNamaSaklar1);
		etNamaSaklar2 = (EditText) findViewById(R.id.etNamaSaklar2);
		etNamaSaklar3 = (EditText) findViewById(R.id.etNamaSaklar3);
		etNamaSaklar4 = (EditText) findViewById(R.id.etNamaSaklar4);
		etDurasi1 = (EditText) findViewById(R.id.etDurasi1);
		etDurasi2 = (EditText) findViewById(R.id.etDurasi2);
		etDurasi3 = (EditText) findViewById(R.id.etDurasi3);
		etDurasi4 = (EditText) findViewById(R.id.etDurasi4);
		
		btnRehber = (Button) findViewById(R.id.btnRehber);
		btnSend = (Button) findViewById(R.id.btnSend);
		btnSendPerulangan1 = (Button) findViewById(R.id.btnSendPerulangan1);
		btnClearPerulangan1 = (Button) findViewById(R.id.btnClearPerulangan1);
		btnSendPerulangan2 = (Button) findViewById(R.id.btnSendPerulangan2);
		btnClearPerulangan2 = (Button) findViewById(R.id.btnClearPerulangan2);
		btnSendPerulangan3 = (Button) findViewById(R.id.btnSendPerulangan3);
		btnClearPerulangan3 = (Button) findViewById(R.id.btnClearPerulangan3);
		btnSendPerulangan4 = (Button) findViewById(R.id.btnSendPerulangan4);
		btnClearPerulangan4 = (Button) findViewById(R.id.btnClearPerulangan4);
		//rbClose = (RadioButton) findViewById(R.id.rbClose);
		//rbOpen = (RadioButton) findViewById(R.id.rbOpen);
		rlForwardLayout = (RelativeLayout) findViewById(R.id.rlForwardLayout);
		etPhoneNo.setText(getCurrentAddress());
		etNamaSaklar1.setText(getNamaSaklar(1));
		etNamaSaklar2.setText(getNamaSaklar(2));
		etNamaSaklar3.setText(getNamaSaklar(3));
		etNamaSaklar4.setText(getNamaSaklar(4));
		/*
		if (isActive()) {
			rbOpen.setChecked(true);
			rlForwardLayout.setVisibility(View.VISIBLE);
			etPhoneNo.setText(getCurrentAddress());
		} else {
			rlForwardLayout.setVisibility(View.GONE);
			rbClose.setChecked(true);
		}
		*/

		btnRehber.setOnClickListener(this);
		btnSend.setOnClickListener(this);
		btnSendPerulangan1.setOnClickListener(this);
		btnClearPerulangan1.setOnClickListener(this);
		btnSendPerulangan2.setOnClickListener(this);
		btnClearPerulangan2.setOnClickListener(this);
		btnSendPerulangan3.setOnClickListener(this);
		btnClearPerulangan3.setOnClickListener(this);
		btnSendPerulangan4.setOnClickListener(this);
		btnClearPerulangan4.setOnClickListener(this);
		
		/* Switch */
    	switch1 = (Switch) findViewById(R.id.switch1);
    	switch2 = (Switch) findViewById(R.id.switch2);
    	switch3 = (Switch) findViewById(R.id.switch3);
    	switch4 = (Switch) findViewById(R.id.switch4);
    	checkBox1 = (CheckBox) findViewById(R.id.checkBox1); 
    	checkBox2 = (CheckBox) findViewById(R.id.checkBox2); 
    	checkBox3 = (CheckBox) findViewById(R.id.checkBox3); 
    	checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
    	switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    	        // do something, the isChecked will be
    	        // true if the switch is in the On position
    	    	checkBox1.setChecked(true);
    	    }
    	});    	
    	switch2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    	        // do something, the isChecked will be
    	        // true if the switch is in the On position
    	    	checkBox2.setChecked(true);
    	    }
    	});    	
    	switch3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    	        // do something, the isChecked will be
    	        // true if the switch is in the On position
    	    	checkBox3.setChecked(true);
    	    }
    	});    	
    	switch4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    	        // do something, the isChecked will be
    	        // true if the switch is in the On position
    	    	checkBox4.setChecked(true);
    	    }
    	});    	
    	/*----*/
	}
	
    @Override
    public void onBackPressed() {
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case PICK_CONTACT:

				etPhoneNo.setText("");

				Uri contactData = data.getData();
				@SuppressWarnings("deprecation")
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {

					String id = c
							.getString(c
									.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

					String hasPhone = c
							.getString(c
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					if (hasPhone.equalsIgnoreCase("1")) {
						Cursor phones = getContentResolver()
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + id, null, null);
						phones.moveToFirst();
						String cNumber = phones.getString(phones
								.getColumnIndex("data1"));

						cNumber = cNumber.replace("(", "");
						cNumber = cNumber.replace(")", "");
						cNumber = cNumber.replace("-", "");
						cNumber = cNumber.replace(" ", "");

						etPhoneNo.setText(cNumber);
					}

				}
				break;
			}
		} else {

			Log.w(DEBUG_TAG, getString(R.string.smsErrorGetNumber));
		}
	}

	@Override
	public void onClick(View v) {
		if (v == btnRehber) {
			Intent intent = new Intent(Intent.ACTION_PICK,
					ContactsContract.Contacts.CONTENT_URI);
			startActivityForResult(intent, PICK_CONTACT);
		} else if (v == btnSend) {
			if (!etPhoneNo.getText().toString().equalsIgnoreCase("")) {
				saveNumber();
				etPhoneNo.setText(getCurrentAddress());
			} else {
				Toast.makeText(SMSSettingActivity.this,
					R.string.smsNumberEmpty, Toast.LENGTH_LONG).show();
			}
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure to send command?").setPositiveButton("Accept", dialogClickListener)
			    .setNegativeButton("Decline", dialogClickListener).show();
			/*
			if (rbOpen.isChecked()) {
				if (!etPhoneNo.getText().toString().equalsIgnoreCase("")) {
					saveNumber();
					etPhoneNo.setText(getCurrentAddress());
				} else {

					Toast.makeText(SMSSettingActivity.this,
							R.string.smsNumberEmpty, Toast.LENGTH_LONG).show();
				}
			} else {
				etPhoneNo.setText("");
				removeNumber();
			}
			*/
		}else if (v == btnSendPerulangan1){
			iPerulangan = 1;
			if (etDurasi1.getText().toString().equalsIgnoreCase("")) {
				Toast.makeText(SMSSettingActivity.this,
					R.string.scheduleDurasiEmpty, Toast.LENGTH_LONG).show();
			}
			else{
				saveNumber();
				etPhoneNo.setText(getCurrentAddress());
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
				builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerPerulangan1)
				    .setNegativeButton("Decline", dlgClkListenerPerulangan1).show();
			}
		}else if (v == btnClearPerulangan1){
			iPerulangan = 1;
			saveNumber();
			etPhoneNo.setText(getCurrentAddress());
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerHapusPerulangan)
			    .setNegativeButton("Decline", dlgClkListenerHapusPerulangan).show();
		}else if (v == btnSendPerulangan2){
			iPerulangan = 2;
			if (etDurasi2.getText().toString().equalsIgnoreCase("")) {
				Toast.makeText(SMSSettingActivity.this,
					R.string.scheduleDurasiEmpty, Toast.LENGTH_LONG).show();
			}
			else{
				saveNumber();
				etPhoneNo.setText(getCurrentAddress());
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
				builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerPerulangan1)
				    .setNegativeButton("Decline", dlgClkListenerPerulangan1).show();
			}
		}else if (v == btnClearPerulangan2){
			iPerulangan = 2;
			saveNumber();
			etPhoneNo.setText(getCurrentAddress());
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerHapusPerulangan)
			    .setNegativeButton("Decline", dlgClkListenerHapusPerulangan).show();
		}else if (v == btnSendPerulangan3){
			iPerulangan = 3;
			if (etDurasi3.getText().toString().equalsIgnoreCase("")) {
				Toast.makeText(SMSSettingActivity.this,
					R.string.scheduleDurasiEmpty, Toast.LENGTH_LONG).show();
			}
			else{
				saveNumber();
				etPhoneNo.setText(getCurrentAddress());
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
				builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerPerulangan1)
				    .setNegativeButton("Decline", dlgClkListenerPerulangan1).show();
			}
		}else if (v == btnClearPerulangan3){
			iPerulangan = 3;
			saveNumber();
			etPhoneNo.setText(getCurrentAddress());
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerHapusPerulangan)
			    .setNegativeButton("Decline", dlgClkListenerHapusPerulangan).show();
		}else if (v == btnSendPerulangan4){
			iPerulangan = 4;
			if (etDurasi4.getText().toString().equalsIgnoreCase("")) {
				Toast.makeText(SMSSettingActivity.this,
					R.string.scheduleDurasiEmpty, Toast.LENGTH_LONG).show();
			}
			else{
				saveNumber();
				etPhoneNo.setText(getCurrentAddress());
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
				builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerPerulangan1)
				    .setNegativeButton("Decline", dlgClkListenerPerulangan1).show();
			}
		}else if (v == btnClearPerulangan4){
			iPerulangan = 4;
			saveNumber();
			etPhoneNo.setText(getCurrentAddress());
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setMessage("Are you sure to send command?").setPositiveButton("Accept", dlgClkListenerHapusPerulangan)
			    .setNegativeButton("Decline", dlgClkListenerHapusPerulangan).show();
		}
	}

	/*Yes No Alert Listener*/
	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
	    	String sCommand;
	    	Integer iMask = 0;
	    	Integer iSwitch = 0;
	    	switch (which){
	        case DialogInterface.BUTTON_POSITIVE:
	            //Yes button clicked
	        	if(switch1.isChecked())
	        		iSwitch |= 1;
	        	if(switch2.isChecked())
	        		iSwitch |= 2;
	        	if(switch3.isChecked())
	        		iSwitch |= 4;
	        	if(switch4.isChecked())
	        		iSwitch |= 8;
	        	
	        	if(checkBox1.isChecked())
	        		iMask |= 1;
	        	if(checkBox2.isChecked())
	        		iMask |= 2;
	        	if(checkBox3.isChecked())
	        		iMask |= 4;
	        	if(checkBox4.isChecked())
	        		iMask |= 8;
	        	
	        	sCommand = "set_relay_out 4 " + iSwitch.toString() + " " + iMask.toString();

	        	/* Send SMS  */
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(getCurrentAddress(), null,
						sCommand, null, null);
				/*----*/	
				Toast.makeText(SMSSettingActivity.this,
						R.string.textKirimYes, Toast.LENGTH_LONG).show();
				break;

	        case DialogInterface.BUTTON_NEGATIVE:
	            //No button clicked
	        	Toast.makeText(SMSSettingActivity.this,
						R.string.textKirimNo, Toast.LENGTH_LONG).show();
	            break;
	        }
	    }
	};
	DialogInterface.OnClickListener dlgClkListenerPerulangan1 = new DialogInterface.OnClickListener() {
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
	    	String sCommand;
	    	Integer iMask = 0;
	    	Integer iSwitch = 0;
	    	DatePicker dp;
	        TimePicker tp;
	        Spinner spinner;
	        String sPerulangan;
	        int iPerulangan_1;
	        int iBulan;
	        iPerulangan_1 = iPerulangan - 1;
	        String strDurasi;
	    	switch (which){
	        case DialogInterface.BUTTON_POSITIVE:
	        	switch(iPerulangan){
	        	case 1:
	    	    	dp = (DatePicker) findViewById(R.id.datePicker1);
	    	        tp = (TimePicker) findViewById(R.id.timePicker1);
	    	        spinner = (Spinner) findViewById(R.id.spinner1);
	        		strDurasi = etDurasi1.getText().toString();
	    	        break;
	        	case 2:
	    	    	dp = (DatePicker) findViewById(R.id.datePicker2);
	    	        tp = (TimePicker) findViewById(R.id.timePicker2);
	    	        spinner = (Spinner) findViewById(R.id.spinner2);
	        		strDurasi = etDurasi2.getText().toString();
	        		break;
	        	case 3:
	    	    	dp = (DatePicker) findViewById(R.id.datePicker3);
	    	        tp = (TimePicker) findViewById(R.id.timePicker3);
	    	        spinner = (Spinner) findViewById(R.id.spinner3);
	        		strDurasi = etDurasi3.getText().toString();
	        		break;
	        	case 4:
	    	    	dp = (DatePicker) findViewById(R.id.datePicker4);
	    	        tp = (TimePicker) findViewById(R.id.timePicker4);
	    	        spinner = (Spinner) findViewById(R.id.spinner4);
	        		strDurasi = etDurasi4.getText().toString();
	        		break;
	        	default:
	    	    	dp = (DatePicker) findViewById(R.id.datePicker1);
	    	        tp = (TimePicker) findViewById(R.id.timePicker1);
	    	        spinner = (Spinner) findViewById(R.id.spinner1);
	        		strDurasi = etDurasi1.getText().toString();
	        		break;
	        	}
	        	
	    	    sPerulangan = spinner.getSelectedItem().toString();
	        	if (sPerulangan.equals("Hourly"))
	        		sPerulangan = "0";
	        	else if (sPerulangan.equals("Daily"))
	        		sPerulangan = "1";
	        	else if (sPerulangan.equals("Weekly"))
	        		sPerulangan = "2";
	        	else if (sPerulangan.equals("Monthly"))
	        		sPerulangan = "3";
	        	iBulan = dp.getMonth() + 1;
	        	sCommand = "set_schedule " + iPerulangan_1 + " " + sPerulangan + " " + dp.getDayOfMonth() + " " + iBulan  
	        			+ " " + dp.getYear() + " " + tp.getCurrentHour() + " " + tp.getCurrentMinute() 
	        			+ " " + strDurasi;

	        	/* Send SMS  */
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(getCurrentAddress(), null,
						sCommand, null, null);
				/*----*/	
				Toast.makeText(SMSSettingActivity.this,
						R.string.textKirimYes, Toast.LENGTH_LONG).show();
	        	break;
	        case DialogInterface.BUTTON_NEGATIVE:
	            //No button clicked
	        	Toast.makeText(SMSSettingActivity.this,
						R.string.textKirimNo, Toast.LENGTH_LONG).show();
	            break;
	    	}
	    }
	};
	DialogInterface.OnClickListener dlgClkListenerHapusPerulangan = new DialogInterface.OnClickListener() {
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
	    	String sCommand;
	    	DatePicker dp;
	        TimePicker tp;
	        Spinner spinner;
	        String sPerulangan;
	        int iPerulangan_1;
	        int iBulan;
	        iPerulangan_1 = iPerulangan - 1;
	    	switch (which){
	        case DialogInterface.BUTTON_POSITIVE:
		        	sCommand = "set_schedule " + iPerulangan_1 + " " + "255" + " " + "255" + " " + "255"  
        				+ " " + "255" + " " + "255" + " " + "255" 
        				+ " " + "255";

		        	/* Send SMS  */
					SmsManager smsManager = SmsManager.getDefault();
					smsManager.sendTextMessage(getCurrentAddress(), null,
							sCommand, null, null);
					/*----*/	
					Toast.makeText(SMSSettingActivity.this,
							R.string.textKirimYes, Toast.LENGTH_LONG).show();
	        		
	        	break;
	        case DialogInterface.BUTTON_NEGATIVE:
	            //No button clicked
	        	Toast.makeText(SMSSettingActivity.this,
						R.string.textKirimNo, Toast.LENGTH_LONG).show();
	            break;
	    	}
	    }
	};
	/*----*/
	
	private void removeNumber() {
		SharedPreferences prefs = getSharedPreferences(KEY_SHAREDNAME, 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_NUMBER, null);
		editor.commit();

		Toast.makeText(SMSSettingActivity.this, R.string.smsDisabled,
				Toast.LENGTH_LONG).show();
	}

	private void saveNumber() {
		SharedPreferences prefs = getSharedPreferences(KEY_SHAREDNAME, 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(KEY_NUMBER, etPhoneNo.getText().toString());
		editor.putString(KEY_SAKLAR1, etNamaSaklar1.getText().toString());
		editor.putString(KEY_SAKLAR2, etNamaSaklar2.getText().toString());
		editor.putString(KEY_SAKLAR3, etNamaSaklar3.getText().toString());
		editor.putString(KEY_SAKLAR4, etNamaSaklar4.getText().toString());
		editor.commit();

		//Toast.makeText(SMSSettingActivity.this, R.string.smsSent,
		//		Toast.LENGTH_LONG).show();
	}

	@Override
	public void onCheckedChanged(CompoundButton v, boolean isChecked) {
		/*
		if (v == rbClose) {
			if (isChecked) {
				//rlForwardLayout.setVisibility(View.GONE);
			}
		} else if (v == rbOpen) {
			if (isChecked) {
				//rlForwardLayout.setVisibility(View.VISIBLE);
				//etPhoneNo.setText(getCurrentAddress());
			}
		}
		*/
	}

	private String getCurrentAddress() {
		SharedPreferences prefs = getSharedPreferences(KEY_SHAREDNAME, 0);
		String mNumber = prefs.getString(KEY_NUMBER, null);

		if (mNumber != null) {
			return mNumber;
		} else {
			return "";
		}
	}
	private String getNamaSaklar(int iSaklarNum) {
		SharedPreferences prefs = getSharedPreferences(KEY_SHAREDNAME, 0);
		String mSaklarNum1 = prefs.getString(KEY_SAKLAR1, null);
		String mSaklarNum2 = prefs.getString(KEY_SAKLAR2, null);
		String mSaklarNum3 = prefs.getString(KEY_SAKLAR3, null);
		String mSaklarNum4 = prefs.getString(KEY_SAKLAR4, null);
		
		switch(iSaklarNum)
		{
			case 1 : 
				if (mSaklarNum1 != null) {
					return mSaklarNum1;
				} else {
					return "Switch " + Integer.toString(iSaklarNum);
				}
			case 2 : 
				if (mSaklarNum2 != null) {
					return mSaklarNum2;
				} else {
					return "Switch " + Integer.toString(iSaklarNum);
				}
			case 3 : 
				if (mSaklarNum3 != null) {
					return mSaklarNum3;
				} else {
					return "Switch " + Integer.toString(iSaklarNum);
				}
			case 4 : 
				if (mSaklarNum4 != null) {
					return mSaklarNum4;
				} else {
					return "Switch " + Integer.toString(iSaklarNum);
				}
			default :
					return "Switch ";
		}
	}

	private boolean isActive() {
		SharedPreferences prefs = getSharedPreferences(KEY_SHAREDNAME, 0);
		String mNumber = prefs.getString(KEY_NUMBER, null);

		if (mNumber != null) {
			return true;
		} else {
			return false;
		}
	}
}
